package com.example.guiguiapplicatio.fragment.FamilyArchives;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.api.GetFamilyarChivesAllApi;
import com.example.guiguiapplicatio.api.Interface.Api;
import com.example.guiguiapplicatio.api.Interface.GetFamilyarChivesAllCallback;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.FamilyarChivesRecordList;
import com.example.guiguiapplicatio.fragment.FamilyArchives.adapter.FamilyArchivesAddAdapter;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FamilyArchivesAddFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private GetFamilyarChivesAllApi getFamilyarChivesAllApi;
    private FamilyArchivesAddAdapter familyArchivesAddAdapter;
    private RecyclerView recyclerView;
    private LinearLayoutManager manager;
    Button search_btn;
    EditText et_searchtext;
    ImageButton servic_back;
    private Api api;
    private Retrofit retrofit;
    private static OkHttpClient client = null;
    // TODO: Rename and change types of parameters
    private String idCard;
    private String HouseholdId;
    private String FamilyIdcardListSting;

    public FamilyArchivesAddFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BreakContinueFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FamilyArchivesAddFragment newInstance(String param1, String param2) {
        FamilyArchivesAddFragment fragment = new FamilyArchivesAddFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            idCard = getArguments().getString("idCard");
            FamilyIdcardListSting = getArguments().getString("FamilyIdcardList");
            HouseholdId = getArguments().getString("HouseholdId");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_total_detail_family_add, container, false);
        client = new OkHttpClient.Builder().
//                 readTimeout(30, TimeUnit.SECONDS).
//                writeTimeout(30, TimeUnit.SECONDS).
        connectTimeout(6, TimeUnit.SECONDS).build();//用于设置请求超时等限制时间

        retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(getString(R.string.api))
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        api = retrofit.create(Api.class);
        servic_back = root.findViewById(R.id.servic_back);
        search_btn = root.findViewById(R.id.search_btn);
        et_searchtext = root.findViewById(R.id.et_searchtext);
        recyclerView = root.findViewById(R.id.examination_start_list);
        getFamilyarChivesAllApi = new GetFamilyarChivesAllApi(new GetFamilyarChivesAllCallback() {
            @Override
            public void onSuccess(ResponseBody<List<FamilyarChivesRecordList>> responseBody) {

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        manager = new LinearLayoutManager(getContext());
                        recyclerView.setLayoutManager(manager);
                        familyArchivesAddAdapter = new FamilyArchivesAddAdapter(responseBody.getData(), getContext(),idCard,HouseholdId,api);
                        recyclerView.setAdapter(familyArchivesAddAdapter);
                    }
                });
            }

            @Override
            public void onFailure(String errorMessage) {

            }
        });
        getFamilyarChivesAllApi.performFamilyarChivesAllList(FamilyIdcardListSting,"");
        servic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                Navigation.findNavController(v).navigate(R.id.action_FamilyArchivesAddFragment_to_FamilyArchivesFragment,bundle);
            }
        });
        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFamilyarChivesAllApi.performFamilyarChivesAllList(FamilyIdcardListSting,et_searchtext.getText().toString());
            }
        });
        return root;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView=requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }
}
